package com.dreamtour.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dreamtour.entity.Agencies;
import com.dreamtour.entity.Services;
import com.dreamtour.service.IAgenciesService;
import com.dreamtour.service.IServicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xls
 * @since 2024-12-24
 */
@RestController
@RequestMapping("/services")
public class ServicesController {
    @Autowired
    private IServicesService servicesService;

    @RequestMapping("/getByPage")
    public IPage<Services> getByPage(Integer pageNum, Integer pageSize) {
        IPage<Services> page = servicesService.page(new Page<>(pageNum, pageSize));
        return page;
    }

    @RequestMapping("/getByName")
    public List<Services> getByName(String serviceTitle) {
        QueryWrapper<Services> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("serviceTitle", serviceTitle);
        List<Services> list = servicesService.list(queryWrapper);
        return list;
    }

    @GetMapping("/check")
    public ResponseEntity<Services> getServiceDetails(@RequestParam Integer serviceId) {
        Services services = servicesService.findById(serviceId);
        if (services != null) {
            return ResponseEntity.ok(services);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @RequestMapping("/getDailyServices")
    public ResponseEntity<List<Map<String, Object>>> getDailyServices() {
        List<Map<String, Object>> dailyServices = servicesService.getDailyServices();
        return ResponseEntity.ok(dailyServices);
    }

    @GetMapping("/filter")
    public ResponseEntity<Map<String, Object>> filterServices(
            @RequestParam(required = false) String serviceStatus) {

        QueryWrapper<Services> queryWrapper = new QueryWrapper<>();

        // 添加验证状态筛选条件
        if (serviceStatus != null && !serviceStatus.isEmpty()) {
            // 假设qualifyStatus的值是"0", "1", "2"分别代表，已通过，待审核,未通过
            // 这里需要根据实际的数据库字段类型和值进行调整
            queryWrapper.eq("serviceStatus", serviceStatus);
        }

        // 查询符合条件的旅行社
        List<Services> filteredServices = servicesService.list(queryWrapper);

        // 准备返回数据
        Map<String, Object> response = new HashMap<>();
        response.put("services", filteredServices);

        return ResponseEntity.ok(response);
    }

    @RequestMapping("/update")
    public boolean update(@RequestBody Services services) {
        boolean b = servicesService.updateById(services);
        System.out.println("**********************************");
        return b;
    }

    @RequestMapping("/updateByBatch")
    public boolean updateByBatch(@RequestBody List<Services> servicesList) {
        boolean b = servicesService.updateBatchById(servicesList);
        return b;
    }

    @RequestMapping("/getServicesCountByMonth")
    public ResponseEntity<List<Map<String, Object>>> getServicesCountByMonth() {
        List<Map<String, Object>> monthlyCounts = servicesService.countServicesByMonth();

        // 直接返回原始的统计数据列表
        return ResponseEntity.ok(monthlyCounts);
    }
}
